let mysqlConnection = require('./mysqlConfig')
let UserPojo = require('../models/UserPojo')

module.exports = {
    selectUserPassword: function (id) {
        let selectUser = `select password from user where id=${id}`
        return new Promise((resolve,reject) => {
            mysqlConnection.query(selectUser, function (error, results) {
                if(error) {
                    reject(error)
                }else {
                    resolve(results)
                }

            })
        })

    },
    selectAllUsers: function () {
        let selectUserSQL = `select  id,fammily_name,given_name,address,email,signup_time from user`
        return new Promise((resolve,reject) => {
            mysqlConnection.query(selectUserSQL, function (error, results) {
                if(error) {
                    reject(error)
                }else {
                    resolve(results)
                }
            })
        })

    },
    selectUserByID: function (id) {
        let selectUserByIDSQL = `select  id,fammily_name,given_name,address,email,password,signup_time from user where id=${id}`
        return new Promise((resolve,reject) => {
            mysqlConnection.query(selectUserByIDSQL, function (error, results) {
                if(error) {
                    reject(error)
                }else {
                    resolve(results)
                }
            })
        })

    },
    selectUserNameAndPassword: function (id) {


        return new Promise((resolve,reject) => {
            let selectUser = `select fammily_name,given_name,password from user where id=${id}`
            mysqlConnection.query(selectUser, function (error, results) {
                if(error) {
                    reject(error)
                }else {
                    resolve(results)
                }
            })
        })

    },
    insertUser: function (userModel) {
        return new Promise((resolve,reject) => {
            let insertUserSQL = `INSERT INTO tasks.user  (id, fammily_name, given_name,signup_time,address,password, email) 
        VALUES (${userModel.id},'${userModel.fammily_name}','${userModel.given_name}',${userModel.signup_time},'${userModel.address}','${userModel.password}','${userModel.email}')`;
            mysqlConnection.query(insertUserSQL, function (error, results) {
                if(error) {
                    reject(error)
                }else {
                    resolve(results)
                }
            })
        })
    },
    updateUser: function (UserModel) {
        return new Promise((resolve,reject) => {
            let updateUserSQL = `UPDATE tasks.user t SET t.fammily_name = '${UserModel.fammily_name}', t.given_name = '${UserModel.given_name}',  t.email = '${UserModel.email}', t.password = '${UserModel.password}',t.address = '${UserModel.address}' WHERE t.id = ${UserModel.id}`;
            mysqlConnection.query(updateUserSQL, function (error, results) {
                if(error) {
                    reject(error)
                }else {
                    resolve(results)
                }
            })
        })
    }

}
